package dk.ihk.ssgroup5;

import org.apache.commons.lang3.StringEscapeUtils;
import java.io.IOException;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import dk.ihk.ssgroup5.dto.Database;
import dk.ihk.ssgroup5.dto.User;

public class CVServlet extends HttpServlet {
	
	private static final long serialVersionUID = 1L;

	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		
		User user = null;
		
		HttpSession session = request.getSession();
		Object userObject = session.getAttribute("user");
		if(userObject != null){
			user = (User) userObject;			
		}
		else{
			response.sendRedirect("DesignServlet?pageId=1&navId=1");
			return;
		}
		
		Integer userId = user.getUserId();	
		
		String CVText = request.getParameter("CVtext");
		
		
		String sql1 = "select * from cv where id = ?";
		String sql2 = "insert into cv (maintext, id) values (? , ?)";
		String sql3 = "update cv set maintext= ? where id = ?";
		
		Connection con = new Database().newCon();
		
		PreparedStatement ps1;
		PreparedStatement ps2;
		PreparedStatement ps3;
		ResultSet rs;
		
		
		try {
			ps1 = con.prepareStatement(sql1);
			ps2 = con.prepareStatement(sql2);
			ps3 = con.prepareStatement(sql3);
			
			ps1.setInt(1, userId);
			
			rs = ps1.executeQuery();
			if(!rs.next()){ 
				ps2.setString(1, CVText);
				ps2.setInt(2, userId);
				ps2.execute();
			}
			else{
				ps3.setString(1, CVText);
				ps3.setInt(2, userId);
				ps3.execute();
			}
			
		} catch (SQLException sx) {
			System.out.println(sx);
		} finally {
			try {
				con.close();
			} catch (Throwable ignore) {
			}
		}
		
				
		response.sendRedirect(request.getContextPath()	+ "/CVServlet");
			
		return;	
	}
	
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		
		User user = null;
		
		HttpSession session = request.getSession();
		Object userObject = session.getAttribute("user");
		if(userObject != null){
			user = (User) userObject;
		}
		else{
			RequestDispatcher dispatcher = request.getRequestDispatcher ("DesignServlet?pageId=1&navId=1");
			dispatcher.forward(request, response);
			return;		
		}
		
		Integer userId =  user.getUserId();					
		
		String sql = "select * from cv where id = " + userId;

		Connection con = new Database().newCon();
		
		PreparedStatement ps;
		ResultSet rs;
		
		String CVText = null;
		
		try {
			ps = con.prepareStatement(sql);
			rs = ps.executeQuery();
			rs.next();
			CVText = StringEscapeUtils.escapeHtml4(rs.getString("maintext"));	
		} catch (SQLException sx) {
			System.out.println(sx);
		} finally {
			try {
				con.close();
			} catch (Throwable ignore) {
			}
		}
				
		request.setAttribute("CVText", CVText );
		RequestDispatcher dispatcher = request.getRequestDispatcher ("/DesignServlet?pageId=6&navId=2");
		dispatcher.forward(request, response);
		return;			
	}
}